package org.flower.isme.flower.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.flower.isme.common.preview.Preview;
import org.flower.isme.common.response.Page;
import org.flower.isme.common.response.R;
import org.flower.isme.flower.domain.entity.EnvironmentData;
import org.flower.isme.flower.domain.request.EnvironmentDataPageRequest;
import org.flower.isme.flower.service.EnvironmentDataService;
import org.flower.isme.netty.enums.Cmd;
import org.flower.isme.netty.handler.NettyServerHandler;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/environment")
@RequiredArgsConstructor
@Tag(name = "环境")
public class EnvironmentDataController {
    private final EnvironmentDataService environmentDataService;
    private final NettyServerHandler nettyServerHandler;
    /**
     * 上传环境数据
     *
     * @param environmentData 环境数据
     * @return R
     */
    @PostMapping("/insert")
    @Preview
    @Operation(summary = "上传环境数据")
    public R<Void> updateLightData(@RequestBody EnvironmentData environmentData) {
        environmentDataService.save(environmentData);
        nettyServerHandler.send(Cmd.CMD_003, "123456", environmentData);
        return R.ok();
    }


    /**
     * 分页查询环境数据列表
     *
     * @param request 查询信息
     * @return R
     */
    @GetMapping("/page")
    @Preview
    @Operation(summary = "分页查询环境数据列表")
    public R<Page<EnvironmentData>> list(EnvironmentDataPageRequest request) {
        return R.ok(environmentDataService.queryPage(request));
    }

    /**
     * 获取环境数据列表
     *
     * @return R
     */
    @GetMapping("/list")
    @Preview
    @Operation(summary = "获取环境数据列表")
    public R<List<EnvironmentData>> list() {
        return R.ok(environmentDataService.listEnvirmentData());
    }
}
